package com.qiantu.api.db;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;

import com.qiantu.api.db.database.LocalDataBase;
import com.qiantu.api.db.greendao.DeviceBeanDao;
import com.qiantu.api.db.greendao.SceneBeanDao;
import com.qiantu.api.entity.DeviceBaseConfigInfo;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.entity.ProtocolSwitchResultBean;
import com.qiantu.api.entity.RoomBean;
import com.qiantu.api.entity.SceneBean;
import com.qiantu.api.utils.DeviceInfoParseUtil;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * Created by cjc on 2019/5/7
 */
public class
LLDeviceDBManager {

    private final String TAG = "LLDeviceDBManager";
    private volatile static LLDeviceDBManager instance = null;
    private final static Object object = new Object();
    private final DeviceBeanDao mDeviceBeanDao;
    private Context context;

    private LLDeviceDBManager(Context context) {
        this.context = context;
        mDeviceBeanDao = LocalDataBase.getInstance().getDaoSession(context).getDeviceBeanDao();
    }

    public static LLDeviceDBManager getInstance(Context context) {
        if (instance == null) {
            synchronized (object) {
                if (instance == null) {
                    instance = new LLDeviceDBManager(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public void clearFromDB() {
        mDeviceBeanDao.deleteAll();
    }

    /**
     * 从服务器更新设备数据
     *
     * @param deviceList
     */
    public void putInTx(List<DeviceBean> deviceList) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            for (DeviceBean device : deviceList) {
                put(device);
            }
        });
    }

    /**
     * 从服务器更新设备数据
     *
     * @param deviceList
     */
    public void refreshAllDevicesTx(List<DeviceBean> deviceList, HandleListener listener) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            clearFromDB();
            if(deviceList == null){
                return;
            }
            Log.i("lzxzx", "refreshAllDevicesTx:" + deviceList.size());
            for (DeviceBean device : deviceList) {
                if (TextUtils.isEmpty(device.getRoomSerialNo())) {
                    device.setRoomSerialNo(RoomBean.ROOM_COMMON_SERIALNO);
                    device.setLinkType(2);
                }
//              设置设备群组标识，该字段平台不提供需要app手动添加
//                device.setDeviceGroup(0);
                device.setLastSort(device.getSort());
                DeviceBaseConfigInfo deviceBaseConfigInfo = DeviceInfoParseUtil.parseData(context, device.getDeviceType(), DeviceBaseConfigInfo.class);
                device.setDeviceBaseConfigInfo(deviceBaseConfigInfo);
                mDeviceBeanDao.save(device);
            }
        });
    }


    /**
     * 保存或者更新设备
     *
     * @param deviceList
     */
    public void addBatch(List<DeviceBean> deviceList) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            for (DeviceBean device : deviceList) {
                if (TextUtils.isEmpty(device.getRoomSerialNo())) {
                    device.setRoomSerialNo(RoomBean.ROOM_COMMON_SERIALNO);
                    device.setLinkType(2);
                }
                device.setLastSort(device.getSort());
                DeviceBaseConfigInfo deviceBaseConfigInfo = DeviceInfoParseUtil.parseData(context, device.getDeviceType(), DeviceBaseConfigInfo.class);
                device.setDeviceBaseConfigInfo(deviceBaseConfigInfo);
                mDeviceBeanDao.save(device);
            }
        });
    }

    public void updateControllerDevices(String controllerSerialNo, List<ProtocolSwitchResultBean.Controller.DeviceItem> deviceItems, HandleListener listener) {
        new Thread() {
            @Override
            public void run() {
                LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
                    List<DeviceBean> deviceList = getDeviceByControllerSerialNo(controllerSerialNo);
                    for (DeviceBean device : deviceList) {
                        for (ProtocolSwitchResultBean.Controller.DeviceItem deviceItem : deviceItems) {
                            if (deviceItem.getDeviceSerialNo().equals(device.getDeviceSerialNo())) {
                                device.setSort(deviceItem.getSort());
                                device.setLastSort(deviceItem.getSort());
                                device.setDeviceType(deviceItem.getDeviceType());
                                device.setName(deviceItem.getName());
                                device.setIsFavourite(deviceItem.isIsFavourite());
                                device.setImageUrl(deviceItem.getImageUrl());
                                device.setIsDisable(deviceItem.isIsDisable());
                                mDeviceBeanDao.save(device);
                            }
                        }
                    }
                });
                if (listener != null) {
                    listener.onCompleted();
                }
            }
        }.start();
    }

//    public void updateDevicesCustomKeys(List<String> deviceSerialNos, List<DeviceCustomKeyBean.KeyData> customKeyList) {
//        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
//            List<DeviceBean> deviceList = getDevicesByDeviceSerialNos(deviceSerialNos);
//            for (DeviceBean device : deviceList) {
//                List<DeviceCustomKeyBean.KeyData> keyDataList = new ArrayList<>();
//                for(DeviceCustomKeyBean.KeyData keyData : customKeyList){
//                    if(device.getDeviceSerialNo().equals(keyData.getDeviceSerialNo())){
//                        keyDataList.add(keyData);
//                    }
//                }
//                updateDeviceCustomKeys(device.getDeviceSerialNo(), keyDataList);
//            }
//        });
//    }

    public List<DeviceBean> getDevicesByDeviceSerialNos(List<String> deviceSerialNos) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.DeviceSerialNo.in(deviceSerialNos))
                .list();
    }

//    public void updateDeviceCustomKeys(String deviceSerialNo, List<DeviceCustomKeyBean.KeyData> customKeyList){
//        if (!TextUtils.isEmpty(deviceSerialNo)) {
//            DeviceBean device = mDeviceBeanDao.queryBuilder()
//                    .where(DeviceBeanDao.Properties.DeviceSerialNo.eq(deviceSerialNo))
//                    .unique();
//            if (device != null) {
//                device.setDeviceCustomKeys(customKeyList);
//                mDeviceBeanDao.save(device);
//            }
//        }
//    }

    /**
     * 保存或者更新设备
     *
     * @param device
     */
    public void put(DeviceBean device) {
        if (device == null)
            return;
        String serialNo = device.getDeviceSerialNo();
        if (serialNo != null && !TextUtils.isEmpty(serialNo)) {
            DeviceBean oldDevice = mDeviceBeanDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo))
                    .unique();
            if (oldDevice != null) {
                device.setLocalId(oldDevice.getLocalId());
            }
        }
        if (device.getLocalId() == null) {
            DeviceBaseConfigInfo deviceBaseConfigInfo = DeviceInfoParseUtil.parseData(context, device.getDeviceType(), DeviceBaseConfigInfo.class);
            device.setDeviceBaseConfigInfo(deviceBaseConfigInfo);
        }
        mDeviceBeanDao.save(device);
    }

    /**
     * 更新设备
     *
     * @param serialNo
     */
    public void updateDeviceSensitivity(String serialNo, int value) {
        if (!TextUtils.isEmpty(serialNo)) {
            DeviceBean device = mDeviceBeanDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo))
                    .unique();
            if (device != null) {
                device.setSensitivity(value);
                mDeviceBeanDao.save(device);
            }
        }
    }

    public void updateDeviceDoorcontactmode(String serialNo, int value) {
        if (!TextUtils.isEmpty(serialNo)) {
            DeviceBean device = mDeviceBeanDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo))
                    .unique();
            if (device != null) {
                device.getStateData().getSettings().setDoorcontactmode(value);
                mDeviceBeanDao.save(device);
            }
        }
    }

    /**
     * 更新展开
     *
     * @param serialNo
     * @param isSwitch
     */
    public void updateExpand(String serialNo, boolean isSwitch) {
        if (!TextUtils.isEmpty(serialNo)) {
            DeviceBean device = mDeviceBeanDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo))
                    .unique();
            if (device != null) {
                device.setIsSwitch(isSwitch);
                mDeviceBeanDao.save(device);
            }
        }
    }

    /**
     * 更新置顶
     *
     * @param isFavourite
     * @param deviceSerialNo
     * @param roomSerialNo
     */
    public void updateDeviceTop(int type, String deviceSerialNo, String roomSerialNo) {
        if (!TextUtils.isEmpty(deviceSerialNo)) {
            DeviceBean device = mDeviceBeanDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.DeviceSerialNo.eq(deviceSerialNo))
                    .unique();
            List<DeviceBean> devices = null;
            if(type == 2){
                devices = mDeviceBeanDao.queryBuilder()
                        .where(DeviceBeanDao.Properties.IsFavourite.eq(true))
                        .orderAsc(DeviceBeanDao.Properties.Sort)
                        .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                        .list();
                if (device != null && devices.size() > 0) {
                    device.setSort(devices.get(0).getSort() - 1);
                    mDeviceBeanDao.save(device);
                }
            }else if(type == 1){
                devices = mDeviceBeanDao.queryBuilder()
                        .where(DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo))
                        .orderAsc(DeviceBeanDao.Properties.RoomSort)
                        .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                        .list();
                if (device != null && devices.size() > 0) {
                    device.setRoomSort(devices.get(0).getRoomSort() - 1);
                    mDeviceBeanDao.save(device);
                }
            }else if(type == 3){
                devices = mDeviceBeanDao.queryBuilder()
                        .where(DeviceBeanDao.Properties.FloorSerialNo.eq(roomSerialNo))
                        .orderAsc(DeviceBeanDao.Properties.FloorSort)
                        .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                        .list();
                if (device != null && devices.size() > 0) {
                    device.setFloorSort(devices.get(0).getFloorSort() - 1);
                    mDeviceBeanDao.save(device);
                }
            }else{
                devices = mDeviceBeanDao.queryBuilder()
                        .orderAsc(DeviceBeanDao.Properties.FloorGlobalSort)
                        .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                        .list();
                if (device != null && devices.size() > 0) {
                    device.setFloorGlobalSort(devices.get(0).getFloorGlobalSort() - 1);
                    mDeviceBeanDao.save(device);
                }
            }
        }
    }

    /**
     * 更新置顶
     *
     * @param isFavourite
     * @param deviceSerialNos
     */
    public void updateDeviceFavourite(boolean isFavourite, List<String> deviceSerialNos) {
        if (deviceSerialNos != null && deviceSerialNos.size() > 0) {
            List<DeviceBean> devices = mDeviceBeanDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.DeviceSerialNo.in(deviceSerialNos))
                    .orderDesc(DeviceBeanDao.Properties.Sort)
                    .orderDesc(DeviceBeanDao.Properties.DeviceSerialNo)
                    .list();
            for(int i=0; i<devices.size(); i++){
                DeviceBean device = devices.get(i);
                if(device == null){
                    continue;
                }
                device.setIsFavourite(isFavourite);
                if(isFavourite){
                    List<DeviceBean> allDevices = getAll();
                    if (allDevices.size() > 0) {
                        device.setSort(allDevices.get(0).getSort() - 1);
                    }
                }else{
                    device.setSort(device.getLastSort());
                }
                mDeviceBeanDao.save(device);
            }
        }
    }

    /**
     * 更新控制器停启用
     *
     * @param serialNo
     * @param isDisable
     */
    public void updateDisable(String serialNo, boolean isDisable) {
        if (!TextUtils.isEmpty(serialNo)) {
            DeviceBean oldDevice = mDeviceBeanDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo))
                    .unique();
            if (oldDevice != null) {
                oldDevice.setIsDisable(isDisable);
                mDeviceBeanDao.save(oldDevice);
            }
        }
    }

    public boolean updateDisableByControllerSerialNo(String controllerSerialNo, boolean isDisable) {
        if (!TextUtils.isEmpty(controllerSerialNo)) {
            List<DeviceBean> devices = mDeviceBeanDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo))
                    .list();
            if (devices != null && devices.size() > 0) {
                for (DeviceBean deviceBean : devices) {
                    deviceBean.setIsDisable(isDisable);
                    mDeviceBeanDao.save(deviceBean);
                }
                return true;
            }
        }
        return false;
    }

    public List<String> updateStateTypeByControllerSerialNo(String controllerSerialNo, int stateType) {
        if (!TextUtils.isEmpty(controllerSerialNo)) {
            List<DeviceBean> devices = mDeviceBeanDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo))
                    .list();
            List<String> deviceSerialNos = new ArrayList<>();
            if (devices != null && devices.size() > 0) {
                for (DeviceBean deviceBean : devices) {
                    deviceBean.setStateType(stateType);
                    deviceSerialNos.add(deviceBean.getDeviceSerialNo());
                    mDeviceBeanDao.save(deviceBean);
                }
                return deviceSerialNos;
            }
        }
        return null;
    }

    /**
     * 通过floorNo获取房间
     *
     * @param serialNo
     * @return
     */
    public DeviceBean getDeviceBySerialNo(String serialNo) {
        return mDeviceBeanDao.queryBuilder().where(DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo)).unique();
    }

    /**
     * 通过serialNo获取
     *
     * @param serialNo
     * @return
     */
    public DeviceBean getDeviceByRoomSerialNoAndSerialNo(String roomSerialNo, String serialNo) {
        return mDeviceBeanDao.queryBuilder().where(DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo),
                DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo)).unique();
    }

    /**
     * 通过serialNo获取
     *
     * @param serialNo
     * @return
     */
    public DeviceBean getAvailableDeviceByRoomSerialNoAndSerialNo(String roomSerialNo, String serialNo) {
        return mDeviceBeanDao.queryBuilder().where(DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo),
                DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo),
                DeviceBeanDao.Properties.IsDisable.eq(false)).unique();
    }

    /**
     * 删除设备
     *
     * @param device
     */
    public void remove(DeviceBean device) {
        if (device == null)
            return;
        DeviceBean oldDevice = mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.DeviceSerialNo.eq(device.getDeviceSerialNo()))
                .unique();
        if (oldDevice != null) {
            mDeviceBeanDao.delete(oldDevice);
        }
    }

    /**
     * 删除设备by controllerId
     *
     * @param controllerSerialNo
     */
    public boolean removeByControllerSerialNo(String controllerSerialNo) {
        if (TextUtils.isEmpty(controllerSerialNo))
            return false;
        List<DeviceBean> devices = mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo))
                .list();
        if (devices != null && devices.size() > 0) {
            for (DeviceBean device : devices) {
                mDeviceBeanDao.delete(device);
            }
            return true;
        }
        return false;
    }

    /**
     * 获得本地所有设备图片列表
     *
     * @return
     */
    public List<DeviceBean> getAll() {
        Log.d("sqlite_debug", "start");
        return mDeviceBeanDao.queryBuilder()
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }
    public List<DeviceBean> getAll(Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 根据大类获得本地所有设备图片列表
     *
     * @return
     */
    public List<DeviceBean> getAvailableDeviceByMainType(String mainType) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.Type.eq(mainType), DeviceBeanDao.Properties.IsDisable.eq(false))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 根据大类获得本地所有设备图片列表
     *
     * @return
     */
    public List<DeviceBean> getDeviceByMainType(String mainType, Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        queryBuilder.where(DeviceBeanDao.Properties.Type.eq(mainType));
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getDeviceByDeviceType(String deviceType, Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        queryBuilder.where(DeviceBeanDao.Properties.DeviceType.eq(deviceType));
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得本地可触发设备
     *
     * @return
     */
    public List<DeviceBean> getTriggerDevices() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsDisable.eq(false), DeviceBeanDao.Properties.Trigger.eq(1),DeviceBeanDao.Properties.DeviceGroup.eq(0))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得本地可触发设备
     *
     * @return
     */
    public List<DeviceBean> getTriggerDevices(Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.where(DeviceBeanDao.Properties.Trigger.eq(1))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得本地可触发设备
     *
     * @return
     */
    public List<DeviceBean> getActionDevices(Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.where(DeviceBeanDao.Properties.IsMission.eq(1))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getGroupDevices(String controllerSerialNo,Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        queryBuilder.where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo));
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.where(DeviceBeanDao.Properties.DeviceGroup.eq(0))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得房间下某一环境设备
     *
     * @return
     */
    public List<DeviceBean> getRoomEnvironmentDevices(String roomSerialNo, int environmentType) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo),
                        DeviceBeanDao.Properties.IsDisable.eq(false),
                        DeviceBeanDao.Properties.EnvironmentType.eq(environmentType))
                .orderAsc(DeviceBeanDao.Properties.RoomSort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得某一环境设备
     *
     * @return
     */
    public List<DeviceBean> getEnvironmentDevices(int environmentType) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsDisable.eq(false),
                        DeviceBeanDao.Properties.EnvironmentType.eq(environmentType))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得本地所有可用设备
     *
     * @return
     */
    public List<DeviceBean> getAvailableDevices() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsDisable.eq(false))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getHouseAvailableDevices() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsDisable.eq(false))
                .orderAsc(DeviceBeanDao.Properties.FloorGlobalSort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getAvailableDevicesWithType(String type) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsDisable.eq(false))
                .where(DeviceBeanDao.Properties.Type.eq(type))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getAvailableDevicesWithDeviceType(String deviceType) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsDisable.eq(false))
                .where(DeviceBeanDao.Properties.DeviceType.eq(deviceType))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得本地设备动作设备
     *
     * @return
     */
    public List<DeviceBean> getActionDevices() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsDisable.eq(false), DeviceBeanDao.Properties.IsMission.eq(1))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public long getAvailableDeviceCount() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsDisable.eq(false))
                .count();
    }

    /**
     * 获得本地房间设备
     *
     * @return
     */
    public List<DeviceBean> getDeviceByRoom(String roomSerialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo))
                .orderAsc(DeviceBeanDao.Properties.RoomSort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getDeviceByRoomAndType(String roomSerialNo,String type) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo),
                        DeviceBeanDao.Properties.Type.eq(type))
                .orderAsc(DeviceBeanDao.Properties.RoomSort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getDeviceByRoomsAndType(List<String> roomSerialNos,String type) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.RoomSerialNo.in(roomSerialNos),
                        DeviceBeanDao.Properties.Type.eq(type))
                .orderAsc(DeviceBeanDao.Properties.RoomSerialNo)
                .orderAsc(DeviceBeanDao.Properties.RoomSort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得本地房间设备数量
     *
     * @return
     */
    public int getDeviceSizeByRoom(String roomSerialNo) {
        return (int) mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo))
                .count();
    }

    /**
     * 获得本地所有场景图片列表
     *
     * @return
     */
    public List<DeviceBean> getHomeAvailableDeviceByRoom(String roomSerialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo),
                        DeviceBeanDao.Properties.LinkType.eq(1),
                        DeviceBeanDao.Properties.IsDisable.eq(false))
                .orderAsc(DeviceBeanDao.Properties.RoomSort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getHomeAvailableDeviceByFloor(String floorSerialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.FloorSerialNo.eq(floorSerialNo),
                        DeviceBeanDao.Properties.IsDisable.eq(false))
                .whereOr(DeviceBeanDao.Properties.LinkType.eq(3),DeviceBeanDao.Properties.LinkType.eq(1))
                .orderAsc(DeviceBeanDao.Properties.FloorSort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 更改设备房间编号未通用
     *
     * @return
     */
    public void updateCommonDeviceByDeleteRoomSerialNo(String roomSerialNo) {
        List<DeviceBean> devices = mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo))
                .list();
        if (devices != null) {
            for (DeviceBean device : devices) {
                device.setLinkType(2);
                device.setRoomSerialNo(RoomBean.ROOM_COMMON_SERIALNO);
                device.setFloorSerialNo("");
                mDeviceBeanDao.save(device);
            }
        }
    }

    /**
     * 根据控制器获取设备
     *
     * @param controllerSerialNo
     * @return
     */
    public List<DeviceBean> getDeviceByControllerSerialNo(String controllerSerialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo))
                .orderAsc(DeviceBeanDao.Properties.LoadSort)
                .orderAsc(DeviceBeanDao.Properties.Pos)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 根据控制器获取设备 还有baseType
     *
     * @param controllerSerialNo
     * @param baseType
     * @return
     */
    public List<DeviceBean> getDeviceByControllerSerialNoAndBaseType(String controllerSerialNo,String baseType ) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo),
                        DeviceBeanDao.Properties.DeviceType.like((baseType + "%")))
                .orderAsc(DeviceBeanDao.Properties.LoadSort)
                .orderAsc(DeviceBeanDao.Properties.Pos)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 根据控制器获取设备 还有baseType
     *
     * @param controllerSerialNo
     * @param deviceCollectSerialNo
     * @param baseType
     * @return
     */
    public List<DeviceBean> getDeviceByControllerSerialNoAndDeviceCollectSerialNoAndBaseType(String controllerSerialNo,String deviceCollectSerialNo,String baseType ) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo),
                        DeviceBeanDao.Properties.DeviceCollectSerialNo.eq(deviceCollectSerialNo),
                        DeviceBeanDao.Properties.DeviceType.like((baseType + "%")))
                .orderAsc(DeviceBeanDao.Properties.LoadSort)
                .orderAsc(DeviceBeanDao.Properties.Pos)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getDeviceByControllerSerialNoWithEnable(String controllerSerialNo,int isEnviroment) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo),
                        DeviceBeanDao.Properties.IsDisable.eq(0))
                .orderAsc(DeviceBeanDao.Properties.LoadSort)
                .orderAsc(DeviceBeanDao.Properties.Pos)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    //获取kgx壳子启用负载
    public List<DeviceBean> getDeviceByControllerSerial4KGXEnable(String controllerSerialNo,String deviceCollectSerialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo),
                        DeviceBeanDao.Properties.DeviceCollectSerialNo.eq(deviceCollectSerialNo),
                        DeviceBeanDao.Properties.IsDisable.eq(0))
                .orderAsc(DeviceBeanDao.Properties.LoadSort)
                .orderAsc(DeviceBeanDao.Properties.Pos)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }
    //获取kgx壳子所有负载
    public List<DeviceBean> getDeviceByControllerSerial4KGX(String controllerSerialNo,String deviceCollectSerialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo),
                        DeviceBeanDao.Properties.DeviceCollectSerialNo.eq(deviceCollectSerialNo))
                .orderAsc(DeviceBeanDao.Properties.LoadSort)
                .orderAsc(DeviceBeanDao.Properties.Pos)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }


    /**
     * 根据控制器获取设备(无灯组)
     *
     * @param controllerSerialNo
     * @return
     */
    public List<DeviceBean> getDeviceByControllerSerialNoWithoutGroupDevice(String controllerSerialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo),
                        DeviceBeanDao.Properties.DeviceGroup.eq(0),
                        DeviceBeanDao.Properties.IsDisable.eq(false))
                .orderAsc(DeviceBeanDao.Properties.Pos)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 根据控制器获取灯组
     *
     * @param controllerSerialNo
     * @return
     */
    public List<DeviceBean> getDeviceByControllerSerialNoOnlyGroupDevice(String controllerSerialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo),
                        DeviceBeanDao.Properties.DeviceGroup.eq(1))
                .orderAsc(DeviceBeanDao.Properties.Pos)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public void updateDeviceByControllerSerialNo(String controllerSerialNo, List<DeviceBean> deviceBeans) {
        LocalDataBase.getInstance().getDaoSession(context).runInTx(() -> {
            List<DeviceBean> devices = mDeviceBeanDao.queryBuilder()
                    .where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo))
                    .list();
            mDeviceBeanDao.deleteInTx(devices);
            for (DeviceBean deviceBean : deviceBeans) {
                mDeviceBeanDao.save(deviceBean);
            }
        });
    }

    /**
     * 获得本地常用设备
     *
     * @return
     */
    public List<DeviceBean> getFavouriteDevices() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsFavourite.eq(true))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得本地常用设备数量
     *
     * @return
     */
    public long getFavouriteDeviceCount() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsFavourite.eq(true))
                .count();
    }
    /**
     * 获得本地所有场景图片列表
     *
     * @return
     */
    public List<DeviceBean> getHomeAvailableFavouriteDevices() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsFavourite.eq(true),
                        DeviceBeanDao.Properties.IsDisable.eq(false))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得本地所有场景图片列表
     *
     * @return
     */
    public DeviceBean getFavouriteDeviceBySerialNo(String serialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsFavourite.eq(true),
                        DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo))
                .unique();
    }

    /**
     * 获得本地所有场景图片列表
     *
     * @return
     */
    public DeviceBean getAvailableFavouriteDeviceBySerialNo(String serialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsFavourite.eq(true),
                        DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo),
                        DeviceBeanDao.Properties.IsDisable.eq(false))
                .unique();
    }

    /**
     * 获得本地所有通用设备列表
     *
     * @return
     */
    public List<DeviceBean> getCommonDevices() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.LinkType.eq(2))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }


    /**
     * 获得本地所有可用的通用设备列表
     *
     * @return
     */
    public List<DeviceBean> getAvailableCommonDevices() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.LinkType.eq(2), DeviceBeanDao.Properties.IsDisable.eq(false))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public int getCommonDeviceSize() {
        return (int) mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.LinkType.eq(2))
                .count();
    }

    public List<DeviceBean> getDevicesByRoomSerialNos(List<String> roomSerialNos) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.RoomSerialNo.in(roomSerialNos))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getDevicesByRoomSerialNos(List<String> roomSerialNos, Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        if(roomSerialNos != null && roomSerialNos.size() > 0){
            queryBuilder.where(DeviceBeanDao.Properties.RoomSerialNo.in(roomSerialNos));
        }
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getDevicesByMainTypeAndRoomSerialNos(String mainType, List<String> roomSerialNos, Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        queryBuilder.where(DeviceBeanDao.Properties.Type.in(mainType));
        if(roomSerialNos != null && roomSerialNos.size() > 0){
            queryBuilder.where(DeviceBeanDao.Properties.RoomSerialNo.in(roomSerialNos));
        }
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getDevicesByDeviceTypeAndRoomSerialNos(String deviceType, List<String> roomSerialNos, Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        queryBuilder.where(DeviceBeanDao.Properties.DeviceType.in(deviceType));
        if(roomSerialNos != null && roomSerialNos.size() > 0){
            queryBuilder.where(DeviceBeanDao.Properties.RoomSerialNo.in(roomSerialNos));
        }
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getAvailableDevicesByRoomSerialNos(List<String> roomSerialNos) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsDisable.eq(false),
                        DeviceBeanDao.Properties.RoomSerialNo.in(roomSerialNos))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得本地通用设备
     *
     * @return
     */
    public List<DeviceBean> getHomeAvailableCommonDevices() {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.IsDisable.eq(false),
                        DeviceBeanDao.Properties.IsFavourite.notEq(true))
                .orderAsc(DeviceBeanDao.Properties.LinkType)
                .orderAsc(DeviceBeanDao.Properties.RoomSort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getTriggerDevicesByRoomSerialNos(List<String> roomSerialNos, Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        queryBuilder.where(DeviceBeanDao.Properties.Trigger.eq(1));
        if(roomSerialNos != null && roomSerialNos.size() > 0){
            queryBuilder.where(DeviceBeanDao.Properties.RoomSerialNo.in(roomSerialNos));
        }
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getActionDevicesByRoomSerialNos(List<String> roomSerialNos, Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        queryBuilder.where(DeviceBeanDao.Properties.IsMission.eq(1));
        if(roomSerialNos != null && roomSerialNos.size() > 0){
            queryBuilder.where(DeviceBeanDao.Properties.RoomSerialNo.in(roomSerialNos));
        }
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    public List<DeviceBean> getGroupDevicesByRoomSerialNos(List<String> roomSerialNos, String controllerSerialNo,Boolean isDisable, Boolean isFavourite, Boolean isHomePage) {
        QueryBuilder<DeviceBean> queryBuilder = mDeviceBeanDao.queryBuilder();
        queryBuilder.where(DeviceBeanDao.Properties.ControllerSerialNo.eq(controllerSerialNo));
        queryBuilder.where(DeviceBeanDao.Properties.DeviceGroup.eq(0));
        if(roomSerialNos != null && roomSerialNos.size() > 0){
            queryBuilder.where(DeviceBeanDao.Properties.RoomSerialNo.in(roomSerialNos));
        }
        if (isDisable != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsDisable.eq(isDisable));
        }
        if (isFavourite != null) {
            queryBuilder.where(DeviceBeanDao.Properties.IsFavourite.eq(isFavourite));
        }
//        if (isHomePage != null) {
//            queryBuilder.where(DeviceBeanDao.Properties.IsHomePage.eq(isHomePage));
//        }
        return queryBuilder.orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }
    public List<DeviceBean> getTriggerDevicesByRoomSerialNos(List<String> roomSerialNos) {
        return mDeviceBeanDao.queryBuilder().where(DeviceBeanDao.Properties.IsDisable.eq(false),
                DeviceBeanDao.Properties.Trigger.eq(1),
                DeviceBeanDao.Properties.RoomSerialNo.in(roomSerialNos))
                .orderAsc(DeviceBeanDao.Properties.Sort)
                .orderAsc(DeviceBeanDao.Properties.DeviceSerialNo)
                .list();
    }

    /**
     * 获得本地所有场景图片列表
     *
     * @return
     */
    public DeviceBean getCommonDeviceBySerialNo(String serialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.LinkType.eq(2),
                        DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo))
                .unique();
    }

    public DeviceBean getAvailableCommonDeviceBySerialNo(String serialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.LinkType.eq(2),
                        DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo),
                        DeviceBeanDao.Properties.IsDisable.eq(false))
                .unique();
    }

    public List<DeviceBean> getAvailableDevicesByDeviceSerialNos(List<String> deviceSerialNos) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.DeviceSerialNo.in(deviceSerialNos),
                        DeviceBeanDao.Properties.IsDisable.eq(false))
                .list();
    }

    /**
     * 获得设备信息
     *
     * @return
     */
    public DeviceBean get(String serialNo) {
        if (TextUtils.isEmpty(serialNo)) {
            return null;
        }
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.DeviceSerialNo.eq(serialNo))
                .unique();
    }

    public List<DeviceBean> getEnviromentDevicesByRoomSerialNos(String roomSerialNo) {
        return mDeviceBeanDao.queryBuilder()
                .where(DeviceBeanDao.Properties.RoomSerialNo.eq(roomSerialNo),new WhereCondition.StringCondition("ENVIRONMENT_TYPE != 0 GROUP BY ENVIRONMENT_TYPE"))
                .orderAsc(DeviceBeanDao.Properties.EnvironmentType)
                .list();
    }
}
